﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace UMLDesigner.Widgets
{
    public class NoteWidget : UMLWidget
    {
        public NoteWidget()
        {
            notePoints = new Point[5];
            noteCornerPoints = new Point[3];
            selectionLinePoints = new Point[5];
            Size = new Size(160, 90);
            type = UMLWidgetType.NOTE;
        }

        public override void DrawItem(Graphics g)
        {
            Option option = CurrentOption.Instance.Option;

            Size size = g.MeasureString(Content, option.DefaultFont, 80).ToSize();

            Size = new Size(160, Math.Max(90, size.Height + 10));

            Color noteOutlineColor = option.NoteOutlineColor;
            Color noteCornerColor = option.NoteCornerColor;

            g.FillPolygon(new LinearGradientBrush(new Rectangle(Location, Size), noteOutlineColor, Color.White, 45.0f), notePoints);
            g.DrawPolygon(new Pen(noteOutlineColor), notePoints);

            g.FillPolygon(new SolidBrush(noteCornerColor), noteCornerPoints);
            g.DrawPolygon(new Pen(noteOutlineColor), noteCornerPoints);

            g.DrawString(Content, SystemFonts.DefaultFont, new SolidBrush(Color.Black), new Rectangle(Location, Size));
        }

        public override void DrawFocused(Graphics g)
        {
            DrawItem(g);
            Pen stippledPen = new Pen(CurrentOption.Instance.Option.FocusedLineColor, 2);
            DrawSelection(g, stippledPen);
        }

        public override void DrawSelected(Graphics g)
        {
            DrawItem(g);
            Pen stippledPen = new Pen(CurrentOption.Instance.Option.SelectedLineColor);
            DrawSelection(g, stippledPen);
        }

        private void DrawSelection(Graphics g, Pen stippledPen)
        {
            Rectangle rect = new Rectangle(Location, Size);
            float[] pattern = new float[2];
            pattern[0] = 5.0f;
            pattern[1] = 5.0f;
            stippledPen.DashPattern = pattern;

            g.DrawPolygon(stippledPen, selectionLinePoints);
        }

        public string Content
        {
            get;
            set;
        }

        public override Size Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                notePoints[0] = new Point(Location.X, Location.Y);
                notePoints[1] = new Point(Location.X + value.Width - 20, Location.Y);
                notePoints[2] = new Point(Location.X + value.Width, Location.Y + 20);
                notePoints[3] = new Point(Location.X + value.Width, Location.Y + value.Height);
                notePoints[4] = new Point(Location.X, Location.Y + value.Height);

                noteCornerPoints[0] = notePoints[1];
                noteCornerPoints[1] = notePoints[2];
                noteCornerPoints[2] = new Point(Location.X + value.Width - 20, Location.Y + 20);

                selectionLinePoints[0] = new Point(notePoints[0].X - 4, notePoints[0].Y - 4);
                selectionLinePoints[1] = new Point(notePoints[1].X + 2, notePoints[1].Y - 4);
                selectionLinePoints[2] = new Point(notePoints[2].X + 4, notePoints[2].Y - 2);
                selectionLinePoints[3] = new Point(notePoints[3].X + 4, notePoints[3].Y + 4);
                selectionLinePoints[4] = new Point(notePoints[4].X - 4, notePoints[4].Y + 4);
                base.Size = value;
            }
        }

        private Point[] notePoints;
        private Point[] noteCornerPoints;
        private Point[] selectionLinePoints;
    }
}
